//--------------------------------------------------------------------------------------------------------------------
// register the function
 var newRegistery1 = new Array("getPlayerInvestementRepartition", "drawPlayerMtMRepartitionTable" );
 allJsonsRegistry = allJsonsRegistry.concat(newRegistery1);

//--------------------------------------------------------------------------------------------------------------------
// implement the function
function drawPlayerMtMRepartitionTable(playerInvestRep) 
{
	fillStratTable(playerInvestRep);
	fillStratPickList(playerInvestRep);
};

//--------------------------------------------------------------------------------------------------------------------
// fill the table displaying the set percentages in the mtm repartition. the current mtm repartition (which coul be different
// from the set percentages because the market could have moved. and a column to set new percentages.
function fillStratTable(playerTableResp) 
{
    myTable = document.getElementById('playerMtMRepartition_table');
    //clean the table
    for(var i = myTable.rows.length; i > 0;i--)
    {
        myTable.deleteRow(i - 1);
    }
    // add the header
    var headerRow = myTable.insertRow(0);
    var newCell = headerRow.insertCell(0);
    newCell.innerHTML = '<th><div class="myBold">Strategy</div></th>';
    newCell = headerRow.insertCell(1);
    newCell.innerHTML = '<th><div class="myBold">Set MtM perc</div></th>';
    newCell = headerRow.insertCell(2);
    newCell.innerHTML = '<th><div class="myBold">Current MtM perc</div></th>';
    newCell = headerRow.insertCell(3);
    newCell.innerHTML = '<th><div class="myBold">Number of Asset in strategy</div></th>';
    newCell = headerRow.insertCell(4);
    newCell.innerHTML = '<th><div class="myBold">MtM Value</div></th>';
    newCell = headerRow.insertCell(5);
    newCell.innerHTML = '<th><div class="myBold">New perc to set</div></th>';    
        
    //add the values   
    var totalMtM = 0;	
    var totalNBAssets = 0;
    var inputStrHelper ='\" type=\"text\" size=\"10%\" onkeypress=\"ifEnter(this,event,SubmitNewStrategyMtmRep);\" value=\"';
	for (var i = 0; i < playerTableResp.investedStrategyList.length; i++)
	{
        var newRow = myTable.insertRow(i+1);
        var newCell = newRow.insertCell(0);
        var name0 = playerTableResp.investedStrategyList[i].name;
        var rep0 = Math.round( playerTableResp.investedStrategyList[i].playerSetMtmRep*10000 )/100;
        newCell.innerHTML = name0;
        newCell = newRow.insertCell(1);
        newCell.innerHTML = rep0 ;
        newCell = newRow.insertCell(2);
        newCell.innerHTML = Math.round( playerTableResp.investedStrategyList[i].playerCurrentMtmRep*10000 )/100;
        newCell = newRow.insertCell(3);
        var nbAsset = playerTableResp.investedStrategyList[i].playerNumOfAssetInStrat;
        totalNBAssets += nbAsset;
        newCell.innerHTML = Math.round( nbAsset*100 )/100;
        newCell = newRow.insertCell(4);
        var fundValue = playerTableResp.investedStrategyList[i].stratValue;
        totalMtM += fundValue;
        newCell.innerHTML = Math.round( fundValue*100 )/100;
        newCell = newRow.insertCell(5);
        var string0 = '<td> <input name=\"'+name0 + inputStrHelper + rep0+ '\"</input></td>';
        newCell.innerHTML = string0;
	}
	
	// add the line for the total
	var totalRow = myTable.insertRow(1);
	var newCell = totalRow.insertCell(0);
	newCell.innerHTML = '<div class="myBold">Total </div>';
    newCell = totalRow.insertCell(1);
    newCell.innerHTML = '<div class="myBold">100</div>';
    newCell = totalRow.insertCell(2);
    newCell.innerHTML = '<div class="myBold">100</div>';
    newCell = totalRow.insertCell(3);
    newCell.innerHTML = '<div class="myBold">'+ Math.round( totalNBAssets*100 )/100 + '</div>';
    newCell = totalRow.insertCell(4);
    newCell.innerHTML = '<div class="myBold">'+ Math.round( totalMtM*100 )/100 + '</div>';
    newCell = totalRow.insertCell(5);
    newCell.innerHTML = '';
  
};

//--------------------------------------------------------------------------------------------------------------------
// display all valid srategies that are not invested by the player to allow him to set them in the table
function fillStratPickList(playerTableResp)
{
    // create reference to first select
    sel1 = document.getElementById('myUninvestedStratList');
    //get the total item from the dropDownList   
    var intTotalItems = sel1.options.length;
    //loop through the number of items and remove them
    for(var intCounter=intTotalItems;intCounter>=0;intCounter--)
    {
        sel1.remove(intCounter);
    }
    // create the text which indicate what do the option
    sel1.options[sel1.length++] = new Option("pick up new strategies", "pick up new strategies");
    // fill the first dropdown uninvested stategies
     for (var i = 0; i < playerTableResp.uninvestedStratList.length; i++)
     {
        var stratName = playerTableResp.uninvestedStratList[i].name;
        sel1.options[sel1.length++] = new Option( stratName, stratName);
     }
};


//--------------------------------------------------------------------------------------------------------------------
// call the main fonction evalutaing the result of the request first
function drawPlayerMtMRepartitionTableWithPythonReturnEval(playerInvestRep) 
{
	playerInvestRepObj = eval( playerInvestRep.responseText );
    drawPlayerMtMRepartitionTable(playerInvestRepObj[0]);
};

//--------------------------------------------------------------------------------------------------------------------
// when a unvested strategy is selected in the pick list, this will add it to the table (and remove it from the pick list)
function addSelectedStratToTable()
{
    my_stratToAdd = document.getElementById('myUninvestedStratList');
    var args = { 
           type: "POST", 
           url: "/addSelectedStratInTable/", 
           cache: false,
           data: { 'myStratDropList' : my_stratToAdd.value },
           complete:drawPlayerMtMRepartitionTableWithPythonReturnEval,
           error: function(errorThrown)  { document.write(errorThrown.responseText);  }
           };
    $.ajax(args);
    return false;    
};


//--------------------------------------------------------------------------------------------------------------------
// remove all the streategies invested with zero% of the player's mtm  from the table. 
// Those strategies will now appear in the picklist
function removeUninvestedStratFromTable()
{
    my_comment = document.getElementById('id_comment_text');
    var args = { 
           type: "POST", 
           url: "/removeUninvestedStratFromTable/", 
           cache: false, 
           complete:drawPlayerMtMRepartitionTableWithPythonReturnEval,
           error: function(errorThrown){ document.write(errorThrown.responseText);}
           };
    $.ajax(args);
};

//--------------------------------------------------------------------------------------
//submit a new mtm repartition
function SubmitNewStrategyMtmRep() 
{ 
    return submitWithoutReload( 
        '/SubmitNewStrategyMtmRep/',
        $('#playerMtMRepartition_table'),
        '#id_messageDiv',
        'The transaction was perfomed successfully.'
        );
};
